<?php

class Users extends Controller {

	function Users()
	{
		parent::Controller();
		$this->load->model(array('users/Usersmodel','admin/Adminforummodel'));
                $this->load->library('YB_Board');
	}
	
	function index()
	{
		$this->load->view('welcome_message');
	}
	function register()
	{
	
	    $rules['member_name'] = "required|min_length[4]|max_length[20]|callback__check_username";
		$rules['member_password'] = "required|matches[retype_password]|min_length[5]|max_length[20]|trim";
		$rules['retype_password']="required";
		$rules['member_email'] = "required|valid_email";
		
		$this->validation->set_rules($rules);
		
		//Text for posted items. 
		$fields['member_name'] = 'User Name';
		$fields['display_name'] = 'Display Name';
		$fields['member_password'] = 'Password';
		$fields['retype_password'] = 'Retype Password';
		$fields['member_email'] = 'Email ID';
		$fields['member_url'] = 'Member URL';
		$fields['location'] = 'Location';
		$this->validation->set_fields($fields);
					
		if ($this->validation->run() == FALSE)
		{	$this->load->view('users/register');}
		else
		{
			$this->load->model('users/Usersmodel');
			$this->Usersmodel->user_register(); 
			
		}
	}//end of register function
	
	// User Login
    function login() {
       $login = $this->session->userdata('logged_in');
		if($login)
		{		 redirect('board','refresh'); }
		
	 		
        $rules['username'] = 'trim|required|callback__check_login';
        $rules['password'] = 'trim|required';
        $this->validation->set_rules($rules);
        
		$fields['username'] = 'username';
        $fields['password'] = 'password';
        $this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{
			$this->Usersmodel->user_login();
        } 
		else 
		{
			$this->load->view('users/login');
		}
    }
	
	
	//Logout
	function logout()
	{
			$this->session->sess_destroy();
			$info['title']="Information Page";
			$info['header']="Information";
			$info['message']="You are successfully logged out.";
			$info['url']="board";
			$info['time']="2";
			
		  	$this->load->view('info',$info);
		
	}
	
	// Check username/email for uniqueness
    function _check_username($username) 
	{
        $email = $this->validation->member_email;
        $query = $this->db->getwhere('board_members', array('member_name'=>$username), 1, 0);
        if ($query->num_rows() != 0) 
		{
            $this->validation->set_message('_check_username', 'This username has already been registered');
            return FALSE;
        } 
		elseif($email!="") 
		{
            $query = $this->db->getwhere('board_members', array('member_email'=>$email), 1, 0);
            if ($query->num_rows() != 0) 
			{
                $this->validation->set_message('_check_username', 'This email address has already been registered');
                return FALSE;
            }
        }
        return TRUE;
    }
	
	
	

	
	
	function view_profile($user_id)
	{
		$data['title']="Forum";
		$data['profile']=$this->Usersmodel->view_profile($user_id);
		$this->load->view('users/profile',$data);
		 
	
	}
	
	
	function edit_profile($member_id,$page_id=1)
	{
		//$login = $this->session->userdata('stem blog_admin_logged_in');
		//if(!$login)
		//{		 //redirect('admin/login','refresh'); }
		
		
		$rules['member_name'] = "trim|required|min_length[4]|max_length[20]";
		$rules['member_email'] = "trim|required|valid_email|max_length[100]";
		$rules['password'] = "trim|min_length[6]|max_length[20]";
		
		
		$this->validation->set_rules($rules);
		
		if ($this->validation->run()) 
		{    
			$this->load->model('Usersmodel');
			$this->Usersmodel->edit_profile($member_id,$page_id);        
		} 
		else 
		{
	        $this->db->where(array('member_id'=>$member_id));
			$query=$this->db->get('members');
			$data['user']=$query->row_array();	
			$data['title']="Edit User";
			$this->load->view('users/edit_profile',$data);
			//$this->load->view('admin/edit_user',$data);
        }
	}
	
	
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/users.php */